package com.supertange.miniapp.wepet.modules.wepet.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.supertange.miniapp.wepet.common.idworker.Sid;
import com.supertange.miniapp.wepet.common.utils.Assert;
import com.supertange.miniapp.wepet.common.utils.FileUploadUtil;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import com.supertange.miniapp.wepet.common.vo.TopicResult;
import com.supertange.miniapp.wepet.modules.wepet.entity.TopicAnswerEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.TopicEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.UsersEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.*;
import com.supertange.miniapp.wepet.modules.wepet.service.TopicService;
import com.supertange.miniapp.wepet.modules.wepet.service.UserService;
import com.supertange.miniapp.wepet.modules.wepet.utils.TopicUtil;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    WxMaService wxMaService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private UserService userService;

    @GetMapping("/queryTopicAll")
    public AjaxResult queryTopicAll() {
        List<TopicVO> topicVOList = topicService.queryTopicAll();
        return AjaxResult.ok(topicVOList);
    }

    @GetMapping("/queryTopicRefresh")
    public AjaxResult queryTopicRefresh() {
        List<TopicVO> topicVOList = topicService.queryTopicRefresh();
        return AjaxResult.ok(topicVOList);
    }

    @PostMapping("/queryTopicByTopicid")
    public AjaxResult queryTopicByTopicid(String topicid) {
        TopicVO topicVO = topicService.queryTopicByTopicid(topicid);
        return AjaxResult.ok(topicVO);
    }

    @PostMapping("/queryTopicById")
    public AjaxResult queryTopicById(String userid) {
        List<TopicVO> topicVOList = topicService.queryTopicById(userid);
        return AjaxResult.ok(topicVOList);
    }

    @GetMapping("/queryTopicAnswer")
    public AjaxResult queryTopicAnswer(String topicid) {
        List<TopicEnterReturnVO> topicEnterReturnVOList = topicService.queryAllTopicAnswerById(topicid);
        return AjaxResult.ok(topicEnterReturnVOList);
    }

    @PostMapping("/queryUserAnswer")
    public AjaxResult queryUserAnswerByUserid(String userid) {
        List<TopicUserAnswerVO> topicEnterReturnVOList = topicService.queryUserAnswerByUserid(userid);
        return AjaxResult.ok(topicEnterReturnVOList);
    }

    @PostMapping("/queryAnswerUser")
    public AjaxResult queryAnswerUserByUserid(String userid) {
        List<TopicAnswerUserVO> topicEnterReturnVOList = topicService.queryAnswerUserByUserid(userid);
        return AjaxResult.ok(topicEnterReturnVOList);
    }


    @PostMapping("/addTopic")
    public AjaxResult addTopic(String userid,String title,String content,int piclen){
        Assert.isTrue(wxMaService.getSecCheckService().checkMessage(title), "请正确发言");
        Assert.isTrue(wxMaService.getSecCheckService().checkMessage(content), "请正确发言");
        String res = topicService.addTopic(userid,title,content,piclen);
        if(res==null)
            return AjaxResult.errorMsg("发表帖子失败");
        else
            return AjaxResult.ok(res);
    }

    @PostMapping("/addPic")
    public AjaxResult addTopic(@RequestParam MultipartFile file,String topicid,int index){
        topicService.addPic(file,topicid,index);
        return AjaxResult.ok();
    }

    @PostMapping("/addTopicAnswer")
    public AjaxResult addTopicAnswer(String comment,String userid,String topicid){
        Assert.isTrue(wxMaService.getSecCheckService().checkMessage(comment), "请正确发言");
        if(!topicService.addTopicAnswer(userid,topicid,comment)){
            return AjaxResult.errorMsg("评论失败");
        }
        return AjaxResult.ok();
    }

    @PostMapping("/addTopicComment")
    public AjaxResult addTopicComment(String userId,int commentId,int type,String comment,String answerid,String commentUsername){
        Assert.isTrue(wxMaService.getSecCheckService().checkMessage(comment), "请正确发言");
        if(!topicService.addTopicComment(userId,commentId,type,comment,answerid,commentUsername)){
            return AjaxResult.errorMsg("评论失败");
        }
        return AjaxResult.ok();
    }

    @PostMapping("/addTopicFavourate")
    public AjaxResult addTopicFavourate(String topicid){
        topicService.addTopicFavourate(topicid);
        return AjaxResult.ok();
    }

    @PostMapping("/subTopicFavourate")
    public AjaxResult subTopicFavourate(String topicid){
        topicService.subTopicFavourate(topicid);
        return AjaxResult.ok();
    }

    @PostMapping("/addTopicAnswerFavourate")
    public AjaxResult addTopicAnswerFavourate(String answerid){
        topicService.addTopicFavourate(answerid);
        return AjaxResult.ok();
    }


}
